/* rqaval2.p - Approver Maintenance Data Validation Program                 */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.             */
/* $Revision: 1.6 $                                                             */
/*V8:ConvertMode=NoConvert                                                  */
/* REVISION 8.5       LAST MODIFIED: 04/15/97  BY: *J1Q2* Patrick Rowan     */
/* REVISION 8.6       LAST MODIFIED: 05/15/98  BY: *J2GM* Patrick Rowan     */
/* REVISION 8.5       LAST MODIFIED: 07/23/98  BY: *J2TL* Patrick Rowan     */
/* REVISION 9.1       LAST MODIFIED: 08/12/00  BY: *N0KP* myb               */
/* Old ECO marker removed, but no ECO header exists *F0PN*                  */
/* $Revision: 1.6 $       BY: Manisha Sawant       DATE: 10/21/01  ECO: *N14P*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/*!
 ----------------------------------------------------------------------------
 DESCRIPTION: Edits/Validates approver maintenance data fields
              Supports the multi-line Purchase Requisition Module of MFG/PRO.

 Notes:
 1) in_apr_type is used to indicate approver type and determines what data
fields require edits/validation.
============================================================================
!*/
{mfdeclre.i " "}

/* CONSTANTS */
{rqconst.i}

/* VALIDATE DATA FIELDS */

define input parameter in_apr      like rqa_apr no-undo.
define input parameter in_apr_type like rqa_type no-undo.
define input parameter in_sub_from like rqa_sub_from no-undo.
define input parameter in_sub_to   like rqa_sub_to no-undo.
define input parameter in_cc_from  like rqa_cc_from no-undo.
define input parameter in_cc_to    like rqa_cc_to no-undo.
define input parameter in_rvw_level
   like rqa_rvw_level no-undo.
define input parameter in_apr_level
   like rqa_apr_level no-undo.
define input parameter in_alt1_apr like rqa_alt_apr[1] no-undo.
define input parameter in_alt2_apr like rqa_alt_apr[2] no-undo.
define input parameter in_admin_apr
   like rqa_admin_apr no-undo.
define input parameter in_start    like rqa_start no-undo.
define input parameter in_end      like rqa_end no-undo.
define input parameter in_recid    as recid no-undo.

define output parameter out_valid_sub_range
   like mfc_logical no-undo.
define output parameter out_valid_cc_range
   like mfc_logical no-undo.
define output parameter out_valid_rvw_level
   like mfc_logical no-undo.
define output parameter out_valid_apr_level
   like mfc_logical no-undo.
define output parameter out_valid_alt1_apr
   like mfc_logical no-undo.
define output parameter out_active_alt1_apr
   like mfc_logical no-undo.
define output parameter out_valid_alt1_level
   like mfc_logical no-undo.
define output parameter out_valid_alt2_apr
   like mfc_logical no-undo.
define output parameter out_active_alt2_apr
   like mfc_logical no-undo.
define output parameter out_valid_alt2_level
   like mfc_logical no-undo.
define output parameter out_valid_admin_apr
   like mfc_logical no-undo.
define output parameter out_valid_date_range
   like mfc_logical no-undo.
define output parameter out_overlap_start_date
   like mfc_logical no-undo.
define output parameter out_overlap_end_date
   like mfc_logical no-undo.
define output parameter out_email_active
   like mfc_logical no-undo.
define output parameter out_apr_email_found
   like mfc_logical no-undo.
define output parameter out_alt1_email_found
   like mfc_logical no-undo.
define output parameter out_alt2_email_found
   like mfc_logical no-undo.
define output parameter out_admin_email_found
   like mfc_logical no-undo.

define variable work_in_start      like rqa_start no-undo.
define variable work_in_end        like rqa_start no-undo.
define variable work_rqa_start     like rqa_start no-undo.
define variable work_rqa_end       like rqa_start no-undo.

define buffer work_rqa_mstr        for rqa_mstr.

/* INITIALIZE */

assign
   out_valid_sub_range        = no
   out_valid_cc_range         = no
   out_valid_rvw_level        = no
   out_valid_apr_level        = no
   out_valid_alt1_apr         = no
   out_active_alt1_apr        = no
   out_valid_alt1_level       = no
   out_valid_alt2_apr         = no
   out_active_alt2_apr        = no
   out_valid_alt2_level       = no
   out_valid_admin_apr        = no
   out_valid_date_range       = no
   out_overlap_start_date     = no
   out_overlap_end_date       = no
   out_email_active           = no
   out_apr_email_found        = no
   out_alt1_email_found       = no
   out_alt2_email_found       = no
   out_admin_email_found      = no.

/* SUB ACCOUNT */
/* OPTIONAL FOR HORIZONTAL, VERTICAL, JOB APPROVERS */
if in_apr_type = horizontal or
   in_apr_type = vertical or
   in_apr_type = JOB
then do:
   if  in_sub_to   = ""
      and in_sub_from = ""
   then
      out_valid_sub_range = yes.

   else
   /* SUB ACCOUNT RANGE CHECK */
      if  in_sub_from <> ""
      and in_sub_to   >= in_sub_from
      then
         out_valid_sub_range = yes.

end.  /* if in_apr_type */

/* COST CENTER */
/* OPTIONAL FOR HORIZONTAL, VERTICAL, JOB APPROVERS */
if in_apr_type = horizontal or
   in_apr_type = vertical or
   in_apr_type = JOB
then do:

   if  in_cc_to   = ""
      and in_cc_from = ""
   then
      out_valid_cc_range = yes.

   else
   /* COST CENTER RANGE CHECK */
      if  in_cc_from <> ""
      and in_cc_to   >= in_cc_from
      then
         out_valid_cc_range = yes.

end.  /* if in_apr_type */

/* REVIEW LEVEL */
/* OPTIONAL FOR ALL APPROVERS */

if in_rvw_level = ? or
   in_rvw_level = 0
then
   out_valid_rvw_level = yes.
else do:
   if can-find(rql_mstr
      where rql_mstr.rql_level = in_rvw_level)
   then
      out_valid_rvw_level = yes.
end.  /* if in_rvw_level = "" */

/* APPROVAL LEVEL */
/* OPTIONAL FOR ALL APPROVERS */
if in_apr_level = ? or
   in_apr_level = 0
then
   out_valid_apr_level = yes.
else do:
   if can-find(rql_mstr
      where rql_mstr.rql_level = in_apr_level)
   then
      out_valid_apr_level = yes.
end.  /* if in_apr_level = "" */

/* ALTERNATE APPROVER #1 */
/* OPTIONAL FOR ALL APPROVERS */
if in_alt1_apr = ""
then
   assign
      out_valid_alt1_apr   = yes
      out_active_alt1_apr  = yes
      out_valid_alt1_level = yes.
else do:
   /* VALIDATE USER ID */
   if can-find(usr_mstr
      where usr_mstr.usr_userid = in_alt1_apr)
   then
      out_valid_alt1_apr = yes.

   do for work_rqa_mstr:
      for each work_rqa_mstr
         where rqa_apr = in_alt1_apr
         no-lock:

         /* VALIDATE ACTIVE OR FUTURE APPROVER */
         if rqa_end > today or
            rqa_end = ?
         then
            out_active_alt1_apr = yes.

         /* VALIDATE APPROVAL LEVEL */
         if rqa_apr_level >= in_apr_level
         then
            out_valid_alt1_level = yes.

      end.   /* for each work_rqa_mstr */
   end.   /* do for work_rqa_mstr  */
end.  /* if in_alt1_apr = "" */

/* ALTERNATE APPROVER #2 */
/* OPTIONAL FOR ALL APPROVERS */
if in_alt2_apr = ""
then
   assign
      out_valid_alt2_apr   = yes
      out_active_alt2_apr  = yes
      out_valid_alt2_level = yes.
else do:
   /* VALIDATE USER ID */
   if can-find(usr_mstr
      where usr_mstr.usr_userid = in_alt2_apr)
   then
      out_valid_alt2_apr = yes.

   do for work_rqa_mstr:
      for each work_rqa_mstr
         where rqa_apr = in_alt2_apr
         no-lock:

         /* VALIDATE ACTIVE OR FUTURE APPROVER */
         if rqa_end > today or
            rqa_end = ?
         then
            out_active_alt2_apr = yes.

         /* VALIDATE APPROVAL LEVEL */
         if rqa_apr_level >= in_apr_level
         then
            out_valid_alt2_level = yes.

      end.   /* for each work_rqa_mstr */
   end.   /* do for work_rqa_mstr  */
end.  /* if in_alt2_apr = "" */

/* ADMIN APPROVER - OPTIONAL */
/* OPTIONAL FOR ALL APPROVERS */
if in_admin_apr = ""
then
   out_valid_admin_apr = yes.
else do:
   if can-find(usr_mstr
      where usr_mstr.usr_userid = in_admin_apr)
   then
      out_valid_admin_apr = yes.
end.  /* if input rqa_admin_apr = "" */

/* DATE RANGE CHECK */
if in_start = ? or
   in_end = ? or
   in_start <= in_end
then
   out_valid_date_range = yes.

/* DATE RANGE OVERLAP CHECK */
/* CONVERT '?' DATES FOR HI/LOW COMPARISON */
if in_start = ?
then
   work_in_start = low_date.
else
   work_in_start = in_start.

if in_end = ?
then
   work_in_end = hi_date.
else
   work_in_end = in_end.

/* FIND CURRENT rqa_mstr */
find rqa_mstr
   where recid(rqa_mstr) = in_recid.

do for work_rqa_mstr:

   /*  LOOK FOR SIMILAR APPROVERS  */
   for each work_rqa_mstr where
      rqa_apr      = rqa_mstr.rqa_apr      and
      rqa_entity   = rqa_mstr.rqa_entity   and
      rqa_category = rqa_mstr.rqa_category and
      rqa_job      = rqa_mstr.rqa_job      and
      rqa_site     = rqa_mstr.rqa_site     and
      rqa_prod_line= rqa_mstr.rqa_prod_line and
      rqa_sub_from = rqa_mstr.rqa_sub_from and
      rqa_cc_from  = rqa_mstr.rqa_cc_from  and
      rqa_type     = rqa_mstr.rqa_type     and
      recid(work_rqa_mstr) <> recid(rqa_mstr)
      no-lock:

      /* CONVERT '?' DATES FOR HI/LOW COMPARISON  */
      if rqa_start = ?
      then
         work_rqa_start = low_date.
      else
         work_rqa_start = rqa_start.

      if rqa_end = ?
      then
         work_rqa_end = hi_date.
      else
         work_rqa_end = rqa_end.

      /*  IS INPUT START WITHIN EFFECTIVITY RANGE?  */
      if work_in_start >= work_rqa_start and
         work_in_start <= work_rqa_end
      then do:
         out_overlap_start_date = yes.
         leave.
      end.

      /*  IS INPUT END WITHIN EFFECTIVITY RANGE?  */
      if work_in_end >= work_rqa_start and
         work_in_end <= work_rqa_end
      then do:
         out_overlap_end_date = yes.
         leave.
      end.

      /*  IS INPUT RANGE NESTING EFFECTIVITY RANGE?  */
      if work_in_start <= work_rqa_start  and
         work_in_end   >= work_rqa_end
      then do:
         out_overlap_start_date = yes.
         leave.
      end.

   end.   /* for each work_rqa_mstr */
end.   /* do for work_rqa_mstr  */

/* E-MAIL CHECK ADDRESSES*/
if can-find(rqf_ctrl
   where rqf_email_opt <> "1")
then do:
   out_email_active = yes.

   /* APPROVER E-MAIL */
   if can-find(usr_mstr
      where usr_userid = in_apr and
      usr_mail_address <> "")
   then
      out_apr_email_found = yes.

   /* ALTERNATE APPROVER #1 E-MAIL */
   if in_alt1_apr = ""
   then
      out_alt1_email_found = yes.
   else do:
      if can-find(usr_mstr
         where usr_userid = in_alt1_apr and
         usr_mail_address <> "")
      then
         out_alt1_email_found = yes.
   end.  /* if in_alt1_apr = "" */

   /* ALTERNATE APPROVER #2 E-MAIL */
   if in_alt2_apr = ""
   then
      out_alt2_email_found = yes.
   else do:
      if can-find(usr_mstr
         where usr_userid = in_alt2_apr and
         usr_mail_address <> "")
      then
         out_alt2_email_found = yes.
   end.  /* if in_alt2_apr = "" */

   /* ADMIN APPROVER E-MAIL */
   if in_admin_apr = ""
   then
      out_admin_email_found = yes.
   else do:
      if can-find(usr_mstr
         where usr_userid = in_admin_apr and
         usr_mail_address <> "")
      then
         out_admin_email_found = yes.
   end.  /* if in_admin_apr = "" */

end.  /* if can-find (rqf_ctrl) */
